package com.psa.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.psa.component.VoicePlayItem;

import java.util.List;

public class VoiceRecorderAdapter extends BaseAdapter {
    private List<VoicePlayItem> playItems;

    public VoiceRecorderAdapter(List<VoicePlayItem> playItems) {
        this.playItems = playItems;
    }

    @Override
    public int getCount() {
        return playItems == null ? 0 : playItems.size();
    }

    @Override
    public Object getItem(int position) {
        return playItems == null || playItems.size() == 0 ? null :
                (position < 0 ? playItems.get(0) :
                        (position >= playItems.size() ? playItems.get(playItems.size()-1)
                                : playItems.get(position)));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (playItems == null || playItems.size() == 0) return null;
        if (position < 0) position = 0;
        if (position >= playItems.size()) position = playItems.size() - 1;
        return playItems.get(position).getView();
    }
}
